package com.kara.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kara.dao.ClazzDao;
import com.kara.dao.SchoolYeadDao;
import com.kara.entity.Clazz;
import com.kara.entity.SchoolYear;
import com.kara.service.ClazzManager;

@Service("clazzManager")
// @WebService(serviceName = "clazzService", endpointInterface =
// "org.appfuse.service.UserService")
public class ClazzManagerImpl implements ClazzManager {

	@Autowired
	ClazzDao clazzDao;

	@Autowired
	SchoolYeadDao schoolYearDao;

	
	public void setClazzDao(ClazzDao clazzDao) {
		this.clazzDao = clazzDao;
	}

	public List<Clazz> getAllClazz(SchoolYear schoolYear) {
		List<Clazz> clazzes = clazzDao.getClazz(schoolYear);
		return clazzes;
	}

	public Clazz getClazz(long id) {
		return clazzDao.get(id);
	}

	public Clazz saveClazz(Clazz clazz) {
		return clazzDao.save(clazz);
	}

	public boolean removeClazz(long id) {
		return removeClazz(id);
	}
	
	public SchoolYear getCurrentSchoolYear () {
	  return schoolYearDao.getCurrentSchoolYear();
	}
	
	public List<Clazz> getAllClazzForCurrentSchoolYear() {
		SchoolYear currentSchoolYear = schoolYearDao.getCurrentSchoolYear();
		List<Clazz> currentYearClazz = new ArrayList<Clazz>();

		currentYearClazz.addAll(getAllClazz(currentSchoolYear));

		return currentYearClazz;
	}

	/**
	 * @throws IllegalArgumentException
	 */
	public void delete(long id) {
		try {
			clazzDao.remove(id);
		} catch (Exception se) {
			new IllegalArgumentException(se);
		}
	}
	public List<SchoolYear> getAllSchoolYears(){
		List<SchoolYear> all = new ArrayList<SchoolYear>();
		Date currentDate = new Date();
		
		for (SchoolYear s : schoolYearDao.getAll()){
			if (s.getIsCurrentSchoolYear()|| s.getStartDate().after(currentDate)){
				all.add(s);
			}
		}
		
		return all;
	}

	public SchoolYear getSchoolYears(long id) {
		return schoolYearDao.get(id);
	}
}
